<?php
// +----------------------------------------------------------------------
// | 专利产品模型
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-12-3 下午5:27:01
// +----------------------------------------------------------------------
defined('BASEPATH') OR exit('No direct script access allowed');
class Patent_model extends MY_Model{
	
	private $table_name;
	public function __construct(){
		parent::__construct();
		$this->load->helper('common');//加载公共函数
		$this->table_name = 'patent';
	}
	
	
	/**
	 * 执行wher条件 
	 * @param unknown_type $where
	 * @author Baip
	 */
	private function _where($where){
		if ( is_array( $where ) ){
			foreach ( $where as $key => $val ){
				if ( is_array( $val ) ){
					$this->db->where_in($key, $val);
					unset($where[$key]);
				}else{
					$this->db->where($key, $val);
				}
			}
		}
	}
	
	/**
	 * 获取总数
	 * @param unknown_type $where
	 * @param unknown_type $field
	 * @author Baip
	 */
	public function getCount($where, $field="*"){
		$this->_where($where);
		$this->db->select($field);
		$count = $this->db->count_all_results($this->table_name);
		return $count;
	}
	
	/**
	 * 获取多条数据
	 * @param array $where
	 * @param string $field
	 * @param string/int $limit
	 * @param int $offset
	 * @param string $order
	 */
	public function getDataObj($where, $field="*", $limit=15, $offset=0,  $order="id DESC"){
		$this->_where($where);
		$this->db->select($field);
		$this->db->order_by($order);
		$this->db->limit($limit, $offset);
		$res = $this->db->get($this->table_name);
		return $res->result();
	}
	/**
	 * 添加数据
	 * @author Baip
	 */
	public function addData(){
		$data = $this->postData();
		$this->db->insert($this->table_name, $data);
		return $this->db->insert_id();
	}
	
	/**
	 * 接收数据
	 */
	private function postData(){
		$title         = $this->input->post('title', true);//产品标题
		$cate_id       = $this->input->post('catid',true);//专利分类id
		$service_price = $this->input->post('service_price', true);//服务费用
		$office_price  = $this->input->post('office_price', true);//官费类型
		$office        = $this->input->post('office', true);//官费类型价格
		$thumb_id      = $this->input->post('thumb_id', true);//图片id
		$thumb         = $this->input->post('thumb', true);//图片路径 
		$server_intro  = $this->input->post('server_intro',true);//服务介绍
		$process       = $this->input->post('process', true);//办理流程
		$required_information = $this->input->post('required_information', true);//所需资料
		$problem       = $this->input->post('problem', true);//常见问题
		$recommend     = $this->input->post('recommend', true);//是否推荐
		//检测官费类型价格
		if ( is_array( $office ) ){
			foreach ( $office as $key => $val ){
				if ( empty( $val ) || !is_numeric( $val ) ){
					$this->error = '请填写官费价格';
				}
			}
		}
		
		$post_data['title']         = $title;
		$post_data['cate_id']       = $cate_id;
		$post_data['service_price'] = $service_price;
		if ( !empty( $office_price ) ){
			$post_data['office_type']   = implode(",", $office_price);
			$post_data['office_price']  = serialize( $office );
		}
		$post_data['thumb_id']      = $thumb_id;
		$post_data['thumb_path']    = $thumb;
		$post_data['server_instro'] = $server_intro;
		$post_data['process']       = $process;
		$post_data['required_information'] = $required_information;
		$post_data['problem']       = $problem;
		$post_data['recommend']     = intval($recommend) == 1 ? 1 : 0;
		$post_data['addtime']       = time();
		return $post_data;
		
	}
	
	
	/**
	 * 更新专利状态（可以传递更新数据）
	 * @param unknown_type $where
	 * @param unknown_type $data
	 */
	public function updateState($where, $data){
		if ( empty( $where ) ){
			return false;
		}
		if ( !is_array( $data ) ) return false;
		$this->_where($where);
		$querey = $this->db->update($this->table_name, $data);
		return $querey;
	}
	
	/**
	 * 更新数据
	 * @param unknown_type $where
	 */
	public function updateData($where){
		if ( empty( $where ) ){
			return false;
		}
		$data_arr = $this->postData();
		$query = $this->db->where($where)->update($this->table_name,$data_arr);
		return $query;
	}
	
	/**
	 * 获取单条数据
	 * @param unknown_type $where
	 * @param unknown_type $field
	 * @param unknown_type $order
	 * @param unknown_type $limit
	 */
	public function getFind($where, $field="*", $order="id DESC", $limit=1 ){
		//if ( empty( $where ) ) return false;
		$this->_where($where);
		$this->db->from($this->table_name);
		$this->db->select($field);
		$this->db->limit($limit);
		$this->db->order_by($order);
		$res = $this->db->get();
		return $res->row();
	}
	
	
	
}